﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EntityInterfaces;

namespace DAL
{
    public class Order : IOrder
    {
        private shopnetEntities model;
        private order entity;
        private List<ordered_carts> cart_item;
       
        public Order() {
            model = new shopnetEntities();
            cart_item = new List<ordered_carts>();
        }

        public void AddItem(int index, int count) {
            var order_cart = new ordered_carts();
            var prd = new Product();
            prd.Read(index);
            order_cart.productID = prd.id;
            order_cart.Price = prd.price * count;
            order_cart.Quantity = count;
            order_cart.orderID = orderID;
            order_cart.name = String.Format("[{0}] {1}", prd.product_code, prd.name);
            model.ordered_carts.AddObject(order_cart);
            Commit();
        }

        public Dictionary<IProduct, int> GetItemsList() {
            var ret = new Dictionary<IProduct, int>();
            foreach (var i in cart_item) { 
                var prd = new Product();
                prd.Read(i.productID);
                ret.Add(prd, (int)i.Quantity);
            }
            return ret;
        }

        public int orderID {
            get
            {
                return entity.orderID;
            }
        }

        public DateTime order_time {
            get {
                return (DateTime)entity.order_time;
            }
            set {
                entity.order_time = value;
            }
        }

        public String cust_fname {
            get {
                return entity.cust_firstname;    
            }
            set {
                entity.cust_firstname = value;
            }
        }

        public String cust_sname
        {
            get
            {
                return entity.cust_lastname;
            }
            set
            {
                entity.cust_lastname = value;
            }
        }

        public String cust_email
        {
            get
            {
                return entity.cust_email;
            }
            set
            {
                entity.cust_email = value;
            }
        }

        public String cust_country
        {
            get
            {
                return entity.cust_country;
            }
            set
            {
                entity.cust_country = value;
            }
        }


        public String cust_zip
        {
            get
            {
                return entity.cust_zip;
            }
            set
            {
                entity.cust_zip = value;
            }
        }

        public String cust_state
        {
            get
            {
                return entity.cust_state;
            }
            set
            {
                entity.cust_state = value;
            }
        }

        public String cust_city
        {
            get
            {
                return entity.cust_city;
            }
            set
            {
                entity.cust_city = value;
            }
        }

        public String cust_address
        {
            get
            {
                return entity.cust_address;
            }
            set
            {
                entity.cust_address = value;
            }
        }

        public String cust_phone
        {
            get
            {
                return entity.cust_phone;
            }
            set
            {
                entity.cust_phone = value;
            }
        }



        public void Save()
        {
            model.orders.AddObject(entity);
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception)
            {
                // log
                throw new EntityException.OrderException("Order Save error", exception);
            }
        }
        public void Read(int id)
        {
            try
            {
                entity = model.orders.Single(s => s.orderID == id);
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.OrderException("Order Read error", exception);
            }
            try {
                var list = model.ordered_carts.Where(s=> s.orderID == id);
                foreach (var i in list) {
                    cart_item.Add(i);
                }
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.OrderException("Ordered Carts Read error", exception);
            }
        }
        public void Commit()
        {
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.OrderException("Order Commit error", exception);
            }
        }

    }
}
